package com.adcc.pushXml.utils;

import com.adcc.pushXml.constant.Constant;
import com.adcc.pushXml.core.FlightPos;
import com.adcc.pushXml.core.XmlModel.XMLAttribute;
import org.dom4j.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

/**
 * @author LHB
 * @date 2019/11/22 14:16
 */
@Component
public class ParseMsg{
    private static Logger log = LoggerFactory.getLogger(ParseMsg.class);
    private static  final String NAME="name";

    /**
     * upd port 50000
     *
     * @return
     */
    public static synchronized String getPort500XML(FlightPos flightPos) throws DocumentException {
        Integer seqId = Constant.recvMsgs.size();
        Document document = DocumentHelper.parseText(Constant.PORT500XMLMODE);
        List<Element> parameter = getElements(seqId, document);
        if (parameter != null && parameter.size() > 0) {
            try {
                for (Element e : parameter) {
                    Attribute att = e.attribute(NAME);
                    setUdp2Xml(att, e, flightPos);
                }
            } catch (Exception e) {
                log.error("ParseMsg.getUdp1XmlMode", e);
            }
        }
        return document.asXML();
    }

    /**
     * 获取 port 50001 的xml 报文
     *
     * @param flightPos
     * @return
     * @throws Exception
     */
    public static synchronized String getPort501XmlMode(FlightPos flightPos) throws DocumentException {
        Integer seqId = Constant.recvMsgs.size();
        Document document = DocumentHelper.parseText(Constant.PORT501XMLMODE);
        List<Element> parameter = getElements(seqId, document);
        if (parameter != null && parameter.size() > 0) {
            try {
                for (Element e : parameter) {
                    Attribute att = e.attribute(NAME);
                    setUdp1Xml(att, e, flightPos);
                }
            } catch (Exception e) {
                log.error("ParseMsg.getUdp1XmlMode", e);
            }
        }
        return document.asXML();

    }

    /**
     * 对udp port 50001 的xml 模拟赋值
     *
     * @param att
     * @param e
     */
    private static void setUdp1Xml(Attribute att, Element e, FlightPos flightPos) {
        try {
            if (att != null && XMLAttribute.ALTITUDERATE.toString().equals(att.getValue())) {
                String time = String.valueOf(System.currentTimeMillis());
                e.attribute("time").setValue(time);
            } else if (att != null && XMLAttribute.MAGHEADING.toString().equals(att.getValue())) {
                // getMap(fi);
                Double angle = flightPos.getAngle();
                e.attribute(Constant.VALUE).setValue(String.valueOf(angle));
            } else if (att != null && XMLAttribute.STATICAIRTEMP.toString().equals(att.getValue())) {
                Attribute attribute = e.attribute(Constant.VALUE);
                Double stati = Double.valueOf(attribute.getValue());
                Random random = new Random(10);
                stati = stati + random.nextDouble();
                e.attribute(Constant.VALUE).setValue(String.valueOf(stati));
            } else if (att != null && XMLAttribute.WINDDIRECTION.toString().equals(att.getValue())) {
                Random random = new Random(40);
                e.attribute(Constant.VALUE).setValue(String.valueOf(random.nextDouble()));
            } else if (att != null && XMLAttribute.WINDSPEED.toString().equals(att.getValue())) {
                Random random = new Random(40);
                e.attribute(Constant.VALUE).setValue(String.valueOf(random.nextDouble()));
            }
        } catch (Exception ex) {
            log.error("port50001 xml赋值出现错误", ex);
        }
    }


    /**
     * 对udp port 50001 的xml 模拟赋值
     *
     * @param att
     * @param e
     */
    private static void setUdp2Xml(Attribute att, Element e, FlightPos flightPos) {
        try {
            if (att != null && XMLAttribute.BAROCORRALT.toString().equals(att.getValue())) {
                String time = String.valueOf(System.currentTimeMillis());
                e.attribute("time").setValue(time);
            }
            if (att != null && XMLAttribute.COMPUTEDAIRSPEED.toString().equals(att.getValue())) {
                //计算空速
                Random random = new Random(100);
                e.attribute(Constant.VALUE).setValue(String.valueOf((random.nextDouble() + 200)));
            } else if (att != null && XMLAttribute.TOTALFUELWEIGHT.toString().equals(att.getValue())) {
                //总油重
                Double count = 100.0;
                Double heavyOil = Constant.heavyOil.get(flightPos.getFi());
                if (heavyOil != null && heavyOil != 0) {
                    count = heavyOil - 1;
                }
                //todo记录当前飞机油重
                Constant.heavyOil.put(flightPos.getFi(),count);
                e.attribute(Constant.VALUE).setValue(String.valueOf(count));
            } else if (att != null && XMLAttribute.GPSLATITUDE.toString().equals(att.getValue())) {
                //纬度
                e.attribute(Constant.VALUE).setValue(String.valueOf(flightPos.getLat()));
            } else if (att != null && XMLAttribute.GPSLONGITUDE.toString().equals(att.getValue())) {
                //经度
                e.attribute(Constant.VALUE).setValue(String.valueOf(flightPos.getLon()));
            }
        } catch (Exception ex) {
            log.error("port50000 xml赋值出现错误", e);
        }
    }


    /**
     * get Attribute
     *
     * @throws Exception
     */
    private static List<Element> getElements(Integer seqId, Document document) {
        List<Element> parameter = null;
        try {
            Element element = document.getRootElement();
            Element context = element.element(Constant.CONTEXT);
            List<Element> keys = context.elements();
            //context --》SEQUENCE_ID
            if (keys != null && keys.size() > 0) {
                Attribute attribute = keys.get(0).attribute(Constant.VALUE);
                if (attribute != null) {
                    attribute.setValue(String.valueOf(seqId));
                }
            }
            //修改parameters
            Element parameters = element.element(Constant.PARAMETERS);
            parameter = parameters.elements();
        } catch (Exception e) {
            log.error("xml 解析失败", e);
        }
        return parameter;
    }


}

